package com.mall.manager.authority.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.mall.manager.authority.service.PartService;
import com.mall.manager.authority.service.UsersPartService;
import com.mall.manager.authority.service.UsersService;
import com.mall.untils.Page;

/**
 * 用户角色控制层
 * @ClassName: UsersPartController
 * @Description: 
 * @author:wangwenyue wangwenyue@ide365.com
 * @date 2016年7月11日 上午9:54:59
 */

@Controller
@RequestMapping("/manager")
public class UsersPartController {
	
	@Resource
	private UsersPartService usersPartService;
	@Resource
	private PartService partService;
	@Resource
	private UsersService usersservice;
	
	
	/**
	 * 根据用户id获取所属角色
	 * @param request
	 * @return
	 */
	@RequestMapping("/getPartByUser")
	@ResponseBody
	public String getPartByUser(HttpServletRequest request){
		Map<String, Object> pwsd = WebUtils.getParametersStartingWith(request, "");
		return String.valueOf(JSONArray.fromObject(usersPartService.findUserPart(pwsd)));
	}
	
	
	
	/**
	 * 初始化用户角色页面 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/userspart_querylist")
	public String queryList(HttpServletRequest request, ModelMap modelMap){
		List<Map<String, Object>> findAll = partService.findAll();
		Page page = usersservice.manager_tusers_list(request);
		modelMap.addAttribute("partList", findAll);
		if(null!=page){
	     	modelMap.addAttribute("data", (List<Map<String,Object>>)page.getResult());
	     	int totalpagecount = page.getTotalPageCount();
	     	modelMap.addAttribute("totalpagecount", totalpagecount);
	     	int currentpageno = page.getCurrentPageNo();
	     	modelMap.addAttribute("currentpageno", currentpageno);
	     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
					.getCurrentPageNo() - 1;
	     	modelMap.addAttribute("previouspage", previouspage);
	     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
					.getTotalPageCount() : page.getCurrentPageNo() + 1;
			modelMap.addAttribute("nextpage", nextpage); 
			List<Integer> showNumList = page.getShowNumList();
			modelMap.addAttribute("pagenumlist", showNumList);
     	}
		
		return "view/user_role";
	}
	
	
	/**
	 * 添加关系
	 * @param request
	 * @return
	 */
	@RequestMapping("/add_UserByPart")
	@ResponseBody
	public String add_UserByPart(HttpServletRequest request){
		Map<String, Object> pwsd = WebUtils.getParametersStartingWith(request, "");
		int add_UserByPart = usersPartService.add_UserByPart(pwsd);
		return String.valueOf(add_UserByPart);
	}
	
	
	/**
	 * 根据条件（数据库列名）获取详细信息
	 * @param request
	 * @return
	 */
	public String getUsersPart(HttpServletRequest request){
		Map<String, Object> pwsd = WebUtils.getParametersStartingWith(request, "");
		Map<String, Object> usersPart = usersPartService.getUsersPart(pwsd);
		return String.valueOf(usersPart);
	}
	
	/**
	 * 删除关系需要user_id，part_id
	 * @param request
	 * @return
	 */
	@RequestMapping("/removeUserPart")
	@ResponseBody
	public String removeUserPart(HttpServletRequest request){
		Map<String, Object> pwsd = WebUtils.getParametersStartingWith(request, "");
		int removeUserPart = usersPartService.removeUserPart(pwsd);
		return String.valueOf(removeUserPart);
	}
	
	
	
	
	
	
}
